/* --------------------------------------
SUMMARY: 	- Estimating discete choice models
			- Exporting the estimated parameters to a new dataset
AUTHOR: Thimo De Schouwer (KU LEUVEN)
NOTES: 		- Preference / WTP coefficients vary over wage distribution
DATE CREATED: 16/09/2021
STATA VERSION: MP 17.0
--------------------------------------- */

clear all
drop _all

cd "${dir}\DataCode\Data\Constructed\LISS"
import delimited Clean_LISS.txt, clear

*=========================*
* 1. Reshaping data 	  *
*=========================*
* Job A characterized by: whA flA tcA mwA mwageA
* Job B characterization differs by experiment
* 2.1 First reshape: one observation per experiment

** JOB A (based on actual job)
forvalues i = 1/10{
	gen tca`i' = telecommutea
	gen wha`i' = workhoursa
	gen fla`i' = flexibilitya
	gen mwa`i' = meaninga
	gen hwagea`i' = hrlywagea
	gen mwagea`i' = monthlywagea
}

** JOB B (differs by experiment)
* 1. Workhours
foreach i in 2 3 4 8 9 10{
	gen whb`i' = workhoursa
}

* 2. Flexibility
foreach i in 1 3 4 5 7 10{
	gen flb`i' = flexibilitya
}

* 3. Meaning
foreach i in 4 5 8 10{
	gen mwb`i' = meaningb
}

foreach i in 1 2 3 6 7 9{
	gen mwb`i' = meaninga
}

* 4. Telecommute
foreach i in 3 7 9 10{
	gen tcb`i' = telecommuteb
}

foreach i in 1 2 4 5 6 8 {
	gen tcb`i' = telecommutea
}

* 5. Monthly Wages & Hourly Wages
forvalues i = 1/10{
	gen mwageb`i' = wage_b`i'
}

forvalues i = 1/10{
	gen hwageb`i' = mwageb`i' / (whb`i'*(52/12))
}


* 6. Choices
forvalues i = 1/10{
	gen choicea`i' = 1 if hce`i' == 1
		replace choicea`i' =  0 if hce`i' == 2
	gen choiceb`i' = 1 if hce`i' == 2
		replace choiceb`i' =  0 if hce`i' == 1
}


reshape long wha whb mwagea mwageb hwagea hwageb mwa mwb tca tcb fla flb choicea choiceb, i(nomem_encr) j(experiment)

* 1.2 Second reshape: one observation per choice
reshape long wh mw tc fl choice hwage mwage, i(nomem_encr experiment) j(choicealt a b) string
sort nomem_encr experiment

* Generating log wages and earnings
gen ln_mwage = ln(mwage)
gen ln_hwage = ln(hwage)

* Binary flexibility and hour categories
recode wh (38 = 0) (32 = 1) (20 = 2)
recode fl (0 = 0) (1 2 3 = 1)


drop if choice == .		// drop experiments that weren't conducted

*====================================================*
* 2. 	Estimating Discrete Choice Models			 *
*		Base Model: preferences bary only by Gender	 *
*====================================================*
* Declare choice method data
egen id = group(nomem_encr experiment)
cmset nomem_encr experiment choicealt

* Labels
label var wh "Workhours"
label var ln_hwage "Log hourly wage"
label var fl "Schedule Adaptability"
label var tc "Telecommuting ability"
label var mw "Meaningful work"
label var nomem_encr

label def whl 0 "Full-Time" 1 "Long Part-Time" 2 "Short Part-Time"
label val wh whl

* Results: Overall WTP by gender
foreach g in 0 1{

	eststo DC_`g': cmclogit choice fl tc mw i.wh ln_hwage if woman == `g', vce(cluster nomem_encr)

	* WTP and S.E. with Delta Method
eststo WTP_SE_`g': nlcom (EUR_WTP_SE_fl_`g': (1 - exp(- (_b[fl]) / _b[ln_hwage]))) ///
				(WTP_SE_tc_`g': (1 - exp(- (_b[tc] / _b[ln_hwage])))) ///
				(WTP_SE_mw_`g': (1 - exp(- (_b[mw] / _b[ln_hwage])))) ///
				(WTP_SE_wh1_`g': (1 - exp(- (_b[1.wh] / _b[ln_hwage])))) ///
				(WTP_SE_wh2_`g': (1 - exp(- (_b[2.wh] / _b[ln_hwage])))), post
}


esttab WTP_SE_0 WTP_SE_1 using "$dir\GraphsTables\OnlineAppendix\Table_WTP_NoInteractions.txt", tex varwidth(25) modelwidth(10) b(%5.3f) ///
	rename(WTP_SE_fl_1 "Schedule Adaptability" WTP_SE_fl_0 "Schedule Adaptability" EUR_WTP_SE_fl_1 "Schedule Adaptability" EUR_WTP_SE_fl_0 "Schedule Adaptability" WTP_SE_tc_1 Telecommuting WTP_SE_tc_0 Telecommuting EUR_WTP_SE_tc_1 Telecommuting EUR_WTP_SE_tc_0 Telecommuting WTP_SE_mw_1 Meaning WTP_SE_mw_0 Meaning EUR_WTP_SE_mw_1 Meaning EUR_WTP_SE_mw_0 Meaning WTP_SE_wh1_1 "Long Part-Time" WTP_SE_wh1_0 "Long Part-Time" EUR_WTP_SE_wh1_1 "Long Part-Time" EUR_WTP_SE_wh1_0 "Long Part-Time" WTP_SE_wh2_1 "Short Part-Time" WTP_SE_wh2_0 "Short Part-Time" EUR_WTP_SE_wh2_1 "Short Part-Time" EUR_WTP_SE_wh2_0 "Short Part-Time") star(* 0.05) ///
	addnote("Note: standard errors clustered on the individual level and calculated with delta method.") ///
    eqlabels(none) collabels(none) mtitles("Men" "Women") se nogaps nofloat nonumbers replace
